document.write('<script src="' + context
		+ '/system/tablequery/js/tablehandler.js"></script>');

/**
 * render td renderTQJspListHeader
 * 
 * @param {}
 *            value
 * @param {}
 *            cellmeta
 * @param {}
 *            record
 * @param {}
 *            rowIndex
 * @param {}
 *            columnIndex
 * @param {}
 *            store
 * @return {}
 */
function renderTQJspListHeader(value, cellmeta, record, rowIndex, columnIndex, store) {
	var cm =  getGridCm('gdzctemplist');
	var fieldName = cm.getDataIndex(columnIndex);
	if (fieldName == 'TNAME') {
		var resultString = String.format('<a href="' + context
						+ '/app/gdzctemplate.do?method=edit&id={0}">{1}</a>',
				record.data['ID'], value);

		return resultString;
	} else if (fieldName == 'TDATE') {
		return formatDate(value);
	} else if (fieldName == 'TSTATUS') {
		return (value == -1 ? '<font color="red">已停用</font>' : '已启用');
	} else if (fieldName == 'SP') {
		var resultString = (record.data['TSTATUS'] == '-1'
				? String
						.format(
								'<a href="javascript:void(0);" onClick="javascript:handleStatus(\'{0}\', \'0\', \'确定启用模板\')">启用</a>',
								record.data['ID'])
				: String
						.format(
								'<a href="javascript:void(0);" onClick="javascript:handleStatus(\'{0}\', \'-1\', \'确定停用模板\')">停用</a>',
								record.data['ID']));

		return resultString;
	}
	return value;
}

/**
 * 添加“过滤按钮”
 * 
 * @param {}
 *            key grid's key
 */
function addMeMix(key) {
	var tbar = Ext.getCmp(key + '_toolbar');

	tbar.add('-', {
				text : '<font style="font-size:13px;">新建模板</font>',
				iconCls : 'addtemp',
				handler : function() {
					var url = context + '/app/gdzctemplate.do?method=neu&id='
							+ getParameter('typeId');
					window.location.href = url;
				}
			});

	tbar.add('-', {
				text : '<font style="font-size:13px;">删除模板</font>',
				iconCls : 'deltemp',
				handler : function() {
					handleDelete(key);
				}
			});
}

/**
 * 处理状态
 * 
 * @param {}
 *            id
 * @param {}
 *            status
 */
function handleStatus(id, status, msg) {
	Ext.MessageBox.confirm('提示', msg, function(e) {
				if (e == "yes") {
					var params = {
						ID : id,
						TSTATUS : status
					};

					updateColumn('APP_GDZC_TEMPLATE', 'single', '-1', params,
							function() {
								return true;
							}, function() {
								handleRefresh();
							});
				}
			});
}

/**
 * 删除操作
 * 
 * @param {}
 *            key
 */
function handleDelete(key) {
	var m = A('name', 'chkitem');

	var chkItems = [];

	if (m.length > 0) {
		for (var i = 0; i < m.length; i++) {
			if (m[i].checked) {
				chkItems.push(m[i]);
			}
		}
	}
	var store = Ext.StoreMgr.get('gdzctemplist_store');
	if (chkItems.length > 0) {
		var tempCounter = '';
		var itemCounter = '';
		for (var i = 0; i < chkItems.length; i++) {
			var idx = chkItems[i].value;

			var record = store.getAt(idx);

			createHidden('ID_' + i, record.data['ID']);
			createHidden('TEMPID_' + i, record.data['ID']);

			if (i == chkItems.length - 1) {
				tempCounter += i;
				itemCounter += i;
			} else {
				tempCounter += i + ','
				itemCounter += i + ',';
			}
		}

		createHidden('tempCounter', tempCounter);
		createHidden('itemCounter', itemCounter);

		deleteTable('APP_GDZC_TEMPLATE,APP_GDZC_TEMPITEM', 'multi,multi',
				'tempCounter,itemCounter', function() {
					return true;
				}, function() {
					handleRefresh();
				});
	} else {
		alert('请选择要删除的模板');
	}
}